package com.sxw.hw_student.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxw.hw_student.system.entity.SysMenu;
import com.sxw.hw_student.system.entity.SysRoleMenu;
import com.sxw.hw_student.system.mapper.SysMenuMapper;
import com.sxw.hw_student.system.mapper.SysRoleMenuMapper;
import com.sxw.hw_student.system.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> getAllMenus() {
        return this.list();
    }

    @Override
    public List<SysMenu> getMenusByRoleId(Integer roleId) {
        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(
                new QueryWrapper<SysRoleMenu>().eq("role_id", roleId)
        );
        List<Integer> menuIds = roleMenus.stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
        if (menuIds.isEmpty()) {
            return List.of();
        }
        return this.list(new QueryWrapper<SysMenu>().in("menu_id", menuIds));
    }
}